package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwalT;
import cn.e4j.modular.crm.entity.MmrxT;
import cn.e4j.modular.crm.mapper.MmrxTMapper;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 *  维修收入结算 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-08-30
 */
@Service
public class MmrxTServiceImpl extends ServiceImpl<MmrxTMapper, MmrxT> {

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));

//        List<Dto> listData = this.baseMapper.getMmrxlist(page, dto);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("mmrxTMapper,getMmrxlist",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMmrhgiData(Page<Object> page, Dto dto) {
        if(!StringUtils.isEmpty(dto.getString("STR"))){
            String[] str = dto.getString("STR").split(",");
            List<String> plist = Lists.newArrayList();
            for (String mmrx : str) {
                plist.add(mmrx);
            }
            dto.put("MMRX",plist);
        }
//        List<Dto> listData = this.baseMapper.getMmrhgiData(page, dto);
        List<Dto> listData =InvokeUtil.invokeBeanMethod("mmrxTMapper,getMmrhgiData",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwak(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));

        List<Dto> listData = this.baseMapper.listGwak(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto listGwal(Dto dto) {
        Dto listData = this.baseMapper.listGwal(dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getSysOrg(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getSysOrg(page, dto);
        /*for(int i=0; i<listData.size(); i++){
            Dto qDto = this.baseMapper.getMB002(listData.get(i).getString("BIZ_CODE"));
            if(qDto != null){
                listData.get(i).put("BIZ_CODE_NAME", qDto.getString("MB002"));
            }else {
                listData.get(i).put("BIZ_CODE_NAME", "");
            }
        }*/
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMmrx019(Page<Object> page, Dto dto) {
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
//        List<Dto> listData = this.baseMapper.getGwak(page, dto);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("mmrxTMapper,getGwak",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> listUser(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.listUser(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getOrg(Dto dto) {
        Dto listData = this.baseMapper.getOrgId(dto.getString("ID"));
        if(listData != null){
//            List<Dto> orgData = this.baseMapper.getOrg(listData.getString("ID"));
            List<Dto> orgData = InvokeUtil.invokeBeanMethod("mmrxTMapper,getOrg",listData.getString("ID"));
            if(orgData.size() > 0){
                listData.put("list", orgData.get(0));
            }
            /*if(orgData.size() > 0){
                Dto MB002 = this.baseMapper.getMB002(orgData.get(0).getString("BIZ_CODE"));
                if(MB002 != null){
                    orgData.get(0).put("BIZ_CODE_NAME", MB002.getString("MB002"));
                    listData.put("list", orgData.get(0));
                }
            }*/
        }
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getOrgData(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.listOrg(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> listGwea(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.listGwea(page, dto);
        return listData;
    }

    /**
     * 获取折扣率
     */
    @Transactional(readOnly = true)
    public Dto getMmrx010(Dto dto) {
        Dto outDto = this.baseMapper.getGwak008(dto);
        if(outDto == null){
            outDto = Dtos.newDto();
        }
        Dto gwak012 = this.baseMapper.getGwak012(dto);
        if(gwak012 != null){
            outDto.put("MMRX007",gwak012.getString("GWAK012"));
            outDto.put("MMRX007_NAME",gwak012.getString("GWEA002"));
            outDto.put("GWEA006",gwak012.getString("GWEA006"));
        }else{
            outDto.put("MMRX007","");
            outDto.put("MMRX007_NAME","");
            outDto.put("GWEA006",0);
        }
        Dto aDto = this.baseMapper.selectGwak056(dto);
        if(aDto != null){
            outDto.put("MMRX010",aDto.getString("GWAK056"));
        }else {
            Dto bDto = this.baseMapper.selectGwaf004(dto);
            if(bDto != null){
                outDto.put("MMRX010",bDto.getString("GWAF004"));
            }else {
                outDto.put("MMRX010",1);
            }
        }
        return outDto;
    }

    /**
     * 获取汇率类型
     */
    public Dto getHulvType(Dto inDto) {
        Dto outDto = Dtos.newDto();
        Dto aDto = this.baseMapper.getGwak057(inDto);
        if(aDto != null){
            outDto.put("TYPE",aDto.getString("GWAK057"));
        }else {
            if(AppCxt.getParam("change_rate_type_") != null){
                outDto.put("TYPE", AppCxt.getParam("change_rate_type_"));
            }else {
                outDto.put("TYPE", "");
            }
        }
        return outDto;
    }

    /**
     * 获取汇率
     */
    public Dto getMmrx009(Dto inDto) {
        Dto outDto = Dtos.newDto();
        if(inDto.getString("TYPE").equals( "1")){
            inDto.put("GWEK002", DateUtils.getDate("yyyyMMdd"));
            Dto aDto = this.baseMapper.getGwel005(inDto);
            if(aDto != null){
                outDto.put("MMRX009",aDto.getString("GWEL005"));
            }else {
                outDto.put("MMRX009","1");
            }
        } else {
            inDto.put("GWEK002",DateUtils.getDate("yyyyMM"));
            Dto bDto = this.baseMapper.getGwek005(inDto);
            if(bDto != null){
                outDto.put("MMRX009",bDto.getString("GWEK005"));
            }else {
                outDto.put("MMRX009","1");
            }
        }
        return outDto;
    }

    @Transactional(readOnly = true)
    public Dto getMB002(Dto dto) {
        Dto data = this.baseMapper.getMB002(dto.getString("MMRXPLANT"));
        return data;
    }

    @Transactional(readOnly = true)
    public Dto getDBinfo1(String ID) {
        Dto data = this.baseMapper.getDBinfo1(ID);
        return data;
    }

    @Transactional(readOnly = true)
    public Dto getGwaz(String MMRXLEGAL) {
        Dto data = this.baseMapper.getGwaz(MMRXLEGAL);
        return data;
    }

    @Transactional(readOnly = true)
    public Dto getSumData(Dto dto) {
        Dto data = this.baseMapper.getSumData(dto);
        return data;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMmrxData(Dto dto) {
//        List<Dto> listData = this.baseMapper.getMmrxData(dto);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("mmrxTMapper,getMmrxData",dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getMmrxAllData(Dto dto) {
//        List<Dto> listData = this.baseMapper.getMmrxAllData(dto);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("mmrxTMapper,getMmrxAllData",dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getMmrx005Sum(Dto dto) {
        Dto data = this.baseMapper.getMmrx005Sum(dto);
        return data;
    }

    @Transactional
    public void addData(List<MmrxT> mmrxTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //增加新的明细
        String mmrwTime = DateUtils.formatDate(new Date(), "HH:mm:ss");
        for (int i = 0; i < mmrxTList.size(); i++) {
            MmrxT mmrx = mmrxTList.get(i);
            mmrx.setMMRXSENT(shiroUser.getDBC());
            mmrx.setMMRXSEQ(BigDecimal.valueOf(i+1));
            mmrx.setMMRXACTI("N");//未审核
            mmrx.setMMRXUSER(shiroUser.getID());
            mmrx.setMMRXGRUP(shiroUser.getORG_ID());
            mmrx.setMMRXDATE(LocalDate.now());
            mmrx.setMMRXTIME(mmrwTime);
            mmrx.setMMRXORIG(shiroUser.getORG_ID());
            mmrx.setMMRXORIU(shiroUser.getID());
            mmrx.setMMRXMLRGAL(mmrx.getMMRXLEGAL());
            mmrx.insert();
        }
    }

    @Transactional
    public void editData(List<MmrxT> mmrxTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        MmrxT mmrxTDel = new MmrxT();
        UpdateWrapper mmrxUpdateWrapper = new UpdateWrapper();
        mmrxUpdateWrapper.eq("MMRXSENT", shiroUser.getDBC());
        mmrxUpdateWrapper.eq("MMRXDOCNO", mmrxTList.get(0).getMMRXDOCNO());
        mmrxTDel.delete(mmrxUpdateWrapper);
        //增加新的明细
        String mmrwTime = DateUtils.formatDate(new Date(), "HH:mm:ss");
        for (int i = 0; i < mmrxTList.size(); i++) {
            MmrxT mmrx = mmrxTList.get(i);
            mmrx.setMMRXSENT(shiroUser.getDBC());
            mmrx.setMMRXSEQ(BigDecimal.valueOf(i+1));
            mmrx.setMMRXACTI("N");//未审核
            mmrx.setMMRXMODU(shiroUser.getID());
            mmrx.setMMRXDATE(LocalDate.now());
            mmrx.setMMRXTIME(mmrwTime);
            mmrx.setMMRXMLRGAL(mmrx.getMMRXLEGAL());
            mmrx.insert();
        }
    }

    @Transactional
    public void delete(MmrxT mmrxT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<MmrxT> mmrxTUpdateWrapper = new UpdateWrapper<>();
        mmrxTUpdateWrapper.eq("MMRXSENT", shiroUser.getDBC());
        mmrxTUpdateWrapper.eq("MMRXDOCNO", mmrxT.getMMRXDOCNO());
        mmrxTUpdateWrapper.eq("MMRXSEQ", mmrxT.getMMRXSEQ());
        mmrxT.delete(mmrxTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<MmrxT> mmrxTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for (MmrxT mmrxT : mmrxTList) {
            UpdateWrapper<MmrxT> mmrxTUpdateWrapper = new UpdateWrapper<>();
            mmrxTUpdateWrapper.eq("MMRXSENT", shiroUser.getDBC());
            mmrxTUpdateWrapper.eq("MMRXDOCNO", mmrxT.getMMRXDOCNO());
            mmrxTUpdateWrapper.eq("MMRXSEQ", mmrxT.getMMRXSEQ());
            mmrxT.delete(mmrxTUpdateWrapper);
        }
    }

    @Transactional
    public void updateStatus(List<MmrxT> mmrxTList) {
        for (int i = 0; i < mmrxTList.size(); i++) {
            MmrxT mmrxT = mmrxTList.get(i);
            UpdateWrapper<MmrxT> mmrxTUpdateWrapper = new UpdateWrapper<>();
            mmrxTUpdateWrapper.eq("MMRXSENT", mmrxT.getMMRXSENT());
            mmrxTUpdateWrapper.eq("MMRXDOCNO", mmrxT.getMMRXDOCNO());
            mmrxT.update(mmrxTUpdateWrapper);
        }
    }

    /**
     * 转ERP订单
     */
    @Transactional
    public JSONArray transferERP(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String[] selectionIds = inDto.getString("datas").split(",");
        List list = Arrays.asList(selectionIds);
        JSONArray json = new JSONArray();
        Set set = new HashSet(list);
        String[] rid = (String[]) set.toArray(new String[0]);

        for (String data : rid) {

        }
        return json;
    }

    /**
     * 还原ERP订单
     */
    @Transactional
    public Dto restoreERP(Dto inDto) {
        Dto outDto = Dtos.newDto();
        return outDto;
    }

    /**
     * 客户资料同步
     */
    @Transactional
    public String SyncERP(String gwak001, String MB003) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto userModel = this.baseMapper.getBizCode(shiroUser.getORG_ID());

        //获取gwak001对应的客户资料数据
        Dto info = this.baseMapper.getGwakinfo(gwak001);

        //获取目标数据库
        Dto dbinfo = this.baseMapper.getDBinfo2(MB003);
        Dto outDto = Dtos.newDto();
        outDto.put("MB003", dbinfo.getString("MB003").trim());//获取目标数据库
        outDto.put("MA001", info.getString("GWAK001"));
        outDto.put("MA002", info.getString("GWAKL003"));
        outDto.put("MA003", info.getString("GWAKL004"));
        outDto.put("MA004", info.getString("GWAK050"));
        outDto.put("MA005", info.getString("GWAK031"));
        outDto.put("MA006", info.getString("GWAK013"));
        outDto.put("MA010", info.getString("GWAK007"));
        if (info.getString("GWAK051").equals("NULL")) {
            outDto.put("MA011", "");
        } else {
            outDto.put("MA011", info.getBigDecimal("GWAK051"));
        }
        if (info.getString("GWAK052").equals("NULL")) {
            outDto.put("MA012", "");
        } else {
            outDto.put("MA012", info.getBigDecimal("GWAK052"));
        }
        outDto.put("MA014", info.getString("GWAK008"));
        outDto.put("MA017", info.getString("GWAK023"));
        outDto.put("MA018", info.getString("GWAK024"));
        outDto.put("MA019", info.getString("GWAK021"));
        //outDto.put("MA023", info.getString("gwak028"));//注册地址
        if (info.getString("GWAL018_FP").isEmpty()) {

        } else {
            outDto.put("MA025", info.getString("GWAL018_FP"));//gwal018 where gwal003='I' 发票地址
        }
        if (info.getString("GWAL018_SH").isEmpty()) {

        } else {
            outDto.put("MA027", info.getString("GWAL018_SH"));//gwal018 where gwal003='D' 送货地址
        }
        outDto.put("MA032", "y");
        outDto.put("MA033", 0);
        outDto.put("MA034", 0);
        outDto.put("MA035", "2");
        outDto.put("MA036", 0);
        outDto.put("MA037", "A");
        outDto.put("MA038", "1");
        outDto.put("MA039", "1");
        outDto.put("MA041", "2");
        outDto.put("MA042", "1");
        outDto.put("MA044", 0);
        outDto.put("MA045", 0);
        outDto.put("MA048", "1");
        outDto.put("MA059", 0);
        outDto.put("MA060", 0);
        outDto.put("MA061", 0);
        outDto.put("MA066", "N");
        outDto.put("MA067", 0);
        outDto.put("MA075", "142");
        outDto.put("MA082", "N");
        outDto.put("MA084", "N");
        outDto.put("MA086", "N");
        outDto.put("MA087", "2");
        outDto.put("MA088", "1");
        outDto.put("MA089", "1");
        outDto.put("MA090", 1);
        outDto.put("MA091", 1);
        outDto.put("MA092", 1);
        outDto.put("MA093", 1);
        outDto.put("MA094", 1);
        outDto.put("MA095", 0);
        outDto.put("MA096", "1");
        outDto.put("MA097", "1");
        //抓税率
        if (info.getString("GWEA006") == null || info.getString("GWAK004").isEmpty()) {
            outDto.put("MA101", 0.16);
        } else {
            outDto.put("MA101", info.getBigDecimal("GWEA006"));
        }
        outDto.put("MA103", "1");
        outDto.put("MA104", 0);
        outDto.put("MA106", "N");
        outDto.put("MA109", 0);
        outDto.put("MA110", 0);
        outDto.put("MA111", 0);
        outDto.put("UDF01", info.getString("GWAK004"));
        outDto.put("UDF02", info.getString("GWAK028"));
        outDto.put("UDF03", info.getString("GWAK003"));
        outDto.put("COMPANY", userModel != null ? userModel.getString("BIZ_CODE"): "");
        outDto.put("CREATOR", shiroUser.getBIZ_CODE());
        outDto.put("USR_GROUP", userModel != null ? userModel.getString("BIZ_CODE"): "");
        outDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMddSSSSS"));
        outDto.put("FLAG", 1);

        //暂时直接写入Y
        //String result = "Y";
        String result = "N";
        //将客户资料插入指定ERP数据库
        try {
            int row = this.baseMapper.insertERP(outDto);
            if (row > 0) {
                result = "Y";
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Transactional
    public Dto transferMmrghiData(List<MmrxT> mmrxTList,Dto dto) {
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = Dtos.newDto();
        inDto.put("ID",shiroUser.getORG_ID());
        inDto.put("ENT",dto.getString("ENT"));
        int amount = Integer.parseInt(AppCxt.getParam("amount_digit_"));
        /*String mmrxplant = "";
        String mmrxlegal = "";*/
        String docno = "";
        /*List<Dto> org = this.baseMapper.getOrg(shiroUser.getORG_ID());
        if(org.size() > 0){
            mmrxplant = org.get(0).getString("BIZ_CODE");
            mmrxlegal = org.get(0).getString("ID");
        }*/
        String[] gwakData = dto.getString("gwaklist").split(",");
        for(String gwak : gwakData){
            String[] gwak_org = gwak.split("/");
            String kehu = "";
            String legal = "";
            if(gwak_org.length != 0){
                kehu = gwak_org[0];
                legal = gwak_org[1];
            }
            List<Dto> orgList = this.baseMapper.getOrgByBiz(legal);
            String mmrxdocno = "PSRD" + DateUtils.getDate("yyyyMMddHHmmssSS");
            //根据客户获取币种、税率等默认值
            inDto.put("GWAK001", kehu);
            Dto resultDto = this.getMmrx010(inDto);
            Dto hulvTypeDto = this.getHulvType(inDto);
            inDto.put("TYPE",hulvTypeDto.getString("TYPE"));
            inDto.put("GWEK001",resultDto.getString("GWAK008"));
            Dto mmrx009Dto = this.getMmrx009(inDto);
            //根据客户获取寄送地址等默认值
            Dto qDto = Dtos.newDto();
            qDto.put("GWAL001",kehu);
            qDto.put("GWAL003","D");
            qDto.put("ENT",dto.getString("ENT"));
            Dto gwalD = this.baseMapper.listGwal(qDto); //送货地址
            Dto pDto = Dtos.newDto();
            pDto.put("GWAL001",kehu);
            pDto.put("GWAL003","I");
            pDto.put("ENT",dto.getString("ENT"));
            Dto gwalI = this.baseMapper.listGwal(pDto);//I发票地址
//            Dto gwakDto = this.baseMapper.getGwakData(inDto);
            Dto gwakDto = InvokeUtil.invokeBeanMethod("mmrxTMapper,getGwakData",inDto);
            String mmrx024 = ";;;;;";
            String mmrx025 = ";;";
            String mmrx026 = ";;";
            if(gwakDto != null) {
                mmrx024 = (gwakDto.getString("GWAK028") == null ? "" : gwakDto.getString("GWAK028")) + ";" + (gwakDto.getString("GWAK013") == null ? "" : gwakDto.getString("GWAK013"))
                        + ";" + (gwakDto.getString("GWAK014") == null ? "" : gwakDto.getString("GWAK014")) + ";" + (gwakDto.getString("GWAK004") == null ? "" : gwakDto.getString("GWAK004"))
                        + ";" + (gwakDto.getString("GWAK003") == null ? "" : gwakDto.getString("GWAK003")) + ";" + (gwakDto.getString("GWAK007") == null ? "" : gwakDto.getString("GWAK007"));
            }
            if(gwalD != null){
                mmrx025 = (gwalD.getString("GWAL018") == null ? "" : gwalD.getString("GWAL018"))
                        + ";" + (gwalD.getString("GWAL021") == null ? "" : gwalD.getString("GWAL021"))
                        + ";" + (gwalD.getString("GWAL020") == null ? "" : gwalD.getString("GWAL020"));
            }
            if(gwalI != null){
                mmrx026 = (gwalI.getString("GWAL018") == null ? "" : gwalI.getString("GWAL018"))
                        + ";" + (gwalI.getString("GWAL021") == null ? "" : gwalI.getString("GWAL021"))
                        + ";" + (gwalI.getString("GWAL020") == null ? "" : gwalI.getString("GWAL020"));
            }
            int count = 1;
            for (MmrxT mmrxT : mmrxTList) {
                if(kehu.equals(mmrxT.getMMRX018()) && legal.equals(mmrxT.getMMRXLEGAL())) {
                    mmrxT.setMMRXSENT(shiroUser.getDBC());
                    mmrxT.setMMRXDOCNO(mmrxdocno);
                    mmrxT.setMMRXSEQ(BigDecimal.valueOf(count));
                    mmrxT.setMMRXDOCDATE(LocalDate.now());
                    mmrxT.setMMRX017(shiroUser.getORG_ID());
                    mmrxT.setMMRXPLANT(orgList.size() > 0 ? orgList.get(0).getString("ID") : "");
                    /*mmrxT.setMmrx001(d.getString("mmrh003"));
                    mmrxT.setMmrx002(d.getString("gwam008"));
                    mmrxT.setMmrx003(d.getBigDecimal("mmrh004"));
                    mmrxT.setMmrx004(d.getBigDecimal("mmrh005"));
                    mmrxT.setMmrx005(d.getBigDecimal("mmrh006"));*/
                    mmrxT.setMMRX006(resultDto.getString("GWAK008"));
                    mmrxT.setMMRX007(resultDto.getString("MMRX007"));//税种
                    mmrxT.setMMRX009(mmrx009Dto.getBigDecimal("MMRX009"));//汇率
                    mmrxT.setMMRX010(resultDto.getBigDecimal("MMRX010"));//折扣率
                    mmrxT.setMMRX011(mmrxT.getMMRX005().multiply(resultDto.getBigDecimal("MMRX010")));//原币折扣后
                    mmrxT.setMMRX012(mmrxT.getMMRX004().multiply(mmrx009Dto.getBigDecimal("MMRX009")));//本币单价
                    mmrxT.setMMRX013(mmrxT.getMMRX005().multiply(mmrx009Dto.getBigDecimal("MMRX009")));//本币折扣前
                    mmrxT.setMMRX014(mmrxT.getMMRX011().multiply(mmrx009Dto.getBigDecimal("MMRX009")));//本币折扣后
                    //原币税额
                    if(resultDto.getString("MMRX007") != ""){
                        BigDecimal mmrx015 = mmrxT.getMMRX011().divide(
                                ((new BigDecimal(1).add(resultDto.getBigDecimal("GWEA006").divide(new BigDecimal(100))))
                                        .multiply(resultDto.getBigDecimal("GWEA006").divide(new BigDecimal(100)))),amount, RoundingMode.HALF_UP
                        );
                        BigDecimal mmrx016 = mmrxT.getMMRX014().divide(
                                ((new BigDecimal(1).add(resultDto.getBigDecimal("GWEA006").divide(new BigDecimal(100))))
                                        .multiply(resultDto.getBigDecimal("GWEA006").divide(new BigDecimal(100)))),amount, RoundingMode.HALF_UP
                        );
                        mmrxT.setMMRX015(mmrx015);//原币税额
                        mmrxT.setMMRX016(mmrx016);//本币税额
                    }
                    /*mmrxT.setMMRX018(gwak);
                    mmrxT.setMmrx019(gwak);*/
                    mmrxT.setMMRX020(gwalD == null ? "" : gwalD.getString("GWAL021"));
                    mmrxT.setMMRX021(gwalD == null ? "" : gwalD.getString("GWAL020"));
                    mmrxT.setMMRX023(gwalD == null ? "" : gwalD.getString("GWAL018"));
                    /*mmrxT.setMMRX022(shiroUser.getID());   //改为回报单人员以及回报单人员所在组织
                    mmrxT.setMMRX032(shiroUser.getORG_ID());*/
                    mmrxT.setMMRX024(mmrx024);
                    mmrxT.setMMRX025(mmrx025);
                    mmrxT.setMMRX026(mmrx026);
                    /*mmrxT.setMMRX027(d.getString("mmrhdocno"));
                    mmrxT.setMmrx028(d.getBigDecimal("mmrhseq"));*/
                    mmrxT.setMMRXACTI("N");
                    mmrxT.setMMRXUSER(shiroUser.getID());
                    mmrxT.setMMRXGRUP(shiroUser.getORG_ID());
                    mmrxT.setMMRXDATE(LocalDate.now());
                    mmrxT.setMMRXTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
                    mmrxT.setMMRXORIG(shiroUser.getORG_ID());
                    mmrxT.setMMRXORIU(shiroUser.getID());
                    mmrxT.setMMRXMLRGAL(mmrxT.getMMRXLEGAL());
                    mmrxT.insert();
                    count++;
                }
            }
            docno = docno + mmrxdocno + ",<br/>";
        }
        outDto.put("docno", StringUtils.substringBeforeLast(docno, ","));
        return outDto;
    }

    public Dto getGwal(Dto inDto) {
        Dto outDto = this.baseMapper.listGwal(inDto);
        return outDto;
    }

    public Dto selectGwakByKey(Dto inDto) {
        Dto outDto = this.baseMapper.listGwak_Gwakl(inDto);
        return outDto;
    }

    @Transactional
    public void editGwak(GwakT gwakT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwakT.setGWAKMODU(shiroUser.getID());
        UpdateWrapper gwakTUpdateWrapper = new UpdateWrapper();
        gwakTUpdateWrapper.eq("GWAKENT", shiroUser.getDBC());
        gwakTUpdateWrapper.eq("GWAK001", gwakT.getGWAK001());
        gwakT.update(gwakTUpdateWrapper);
    }

    @Transactional
    public void editGwal(Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWALMODU", shiroUser.getID());
        int count = this.baseMapper.updateGwalData(inDto);
        if(count == 0){
            GwalT gwalT = new GwalT();
            gwalT.setGWALENT(shiroUser.getDBC());
            gwalT.setGWAL001(inDto.getString("GWAK001"));
            Dto maxGwal002 = this.baseMapper.getMaxGwal002(inDto);
            if(maxGwal002 == null){
                gwalT.setGWAL002("1");
            }else {
                gwalT.setGWAL002(maxGwal002.getString("GWAL002"));
            }
            gwalT.setGWAL003(inDto.getString("GWAL003"));
            gwalT.setGWAL010("*");
            gwalT.setGWAL011("*");
            gwalT.setGWAL012("*");
            gwalT.setGWAL013("*");
            gwalT.setGWAL014("*");
            gwalT.setGWAL015("*");
            gwalT.setGWAL016("*");
            gwalT.setGWAL017("*");
            gwalT.setGWAL018(inDto.getString("GWAL018"));
            gwalT.setGWAL019("*");
            gwalT.setGWAL020(inDto.getString("GWAL020"));
            gwalT.setGWAL021(inDto.getString("GWAL021"));
            gwalT.setGWALACTI("Y");
            gwalT.setGWALUSER(shiroUser.getID());
            gwalT.setGWALORIU(shiroUser.getID());
            gwalT.setGWALORIG(shiroUser.getORG_ID());
            gwalT.setGWALGRUP(shiroUser.getORG_ID());
            Date date = new Date();
            gwalT.setGWALDATE(LocalDate.now());
            gwalT.setGWALTIME(DateUtils.formatDate(date, "HH:mm:ss"));
            gwalT.insert();
        }
        if (inDto.getString("GWAL003").equals("Y")) {
            this.baseMapper.updateAddressData(inDto);
        }
    }

    public Dto getGwakData(Dto inDto) {
//        return this.baseMapper.getGwakData(inDto);
        return InvokeUtil.invokeBeanMethod("mmrxTMapper,getGwakData",inDto);
    }
}
